<template>
  <template v-if="false"></template>
</template>

<script lang="ts" setup>
import { inject, onUnmounted, toRefs, watch, type PropType, type Ref } from 'vue'
import type { HookFn, IconMap, RequestParams } from '../types'
import type { BillboardController } from '../utils'

const props = defineProps({
  billboardController: {
    type: Object as PropType<BillboardController>,
    required: true,
  },
  params: {
    type: Object as PropType<RequestParams>,
    required: true,
  },
  hook: {
    type: Function as PropType<HookFn>,
    required: true,
  },
  keepOriginal: {
    type: Boolean,
    default: true,
  },
})
const { billboardController, hook, keepOriginal } = props
const { params } = toRefs(props)
const iconMap = inject<Ref<IconMap>>('iconMap')!

const { executeRequest } = hook(billboardController, iconMap.value)

watch(
  params,
  (params) => {
    executeRequest(params)
    // 是否需要清除原有数据
    if (!keepOriginal) {
      billboardController.clear()
    }
  },
  { immediate: true, deep: true }
)

onUnmounted(() => {
  billboardController.clear()
})
</script>
